var ChangeCode = "2";//验证码是否正确，正确为“1”,不正确为“2”
var PhoneCode = "2";//手机验证码
$(document).ready(function(){
	CheckCode();
	CheckButton();
	SendPhoneCode();
});
//验证码操作
var CheckCode = function(){
	$("img#checkcode").click(function(){
		 var random=Math.random();
		 $(this).attr('src',basePath+'/meb/codeimg?'+random);
	});
	
}
//验证第一个验证码是否正确
var CheckFCode = function(){
	var vcode = $("input#icheckcode").val();
	$.ajax({
        url:basePath+'/meb/checkcode',
        data:{
            code:vcode
        },
        type:'post',
        dataType:'json',
        success:function(data){
        	ChangeCode=data.code;
            if(data.code=="2"){
                
                layer.msg("验证码错误，请重新输入");
            }
            if(data.code=="1"){
            	$("div.forget_stepone").fadeOut();
    			$("div.forget_steptwo").fadeIn();
    			$("div.forget_stepthree").fadeOut();
    			var phone = $("input.forget_step_center_one_two:eq(0)").val();
    			$("input.forget_step_center_one_two:eq(1)").val(phone);
            }
        },
        error:function(data){

        },
        complete:function (data) {

        }
    })
}
//按钮作用
var CheckButton = function(){
	$("input.forget_step_bottom_b:eq(0)").click(function(){
		var phone = $("input.forget_step_center_one_two:eq(0)").val();
		if(phone == null || phone == ""){
			layer.msg("请输入正确的手机号码");
			console.log("phone=")+phone;
			return;
		}
		CheckFCode();
	});
	$("input.forget_step_bottom_one").click(function(){
		$("div.forget_stepone").fadeIn();
		$("div.forget_steptwo").fadeOut();
		$("div.forget_stepthree").fadeOut();
	});
	
	$("input.forget_step_bottom_two").click(function(){		
		CheckPhoneCode();
		
	});
	$("input.forget_step_bottom_b:eq(1)").click(function(){
		var one = $("input.forget_step_center_one_two:eq(2)").val();
		var two = $("input.forget_step_center_one_two:eq(3)").val();
		if(one == "" || two == ""){
			layer.msg("密码不可以为空，请重试");
			return;
		};
		if(one != two){
			layer.msg("2次密码输入不一致，请重试");
			return;
		};
		ChangePhonePW();
	});
	layer.close(LoadingIcon);
}
//发送验证码
var SendPhoneCode = function(){
	$("input.forget_step_center_two_four").click(function(){
	        var phone=$("input.forget_step_center_one_two:eq(1)").val();
	        $.ajax({
	            url:basePath+'/meb/phonecode',
	            data:{
	                phone:phone
	            },
	            type:'post',
	            dataType:'json',
	            success:function(data){
	            	if(data.code == "1"){
	            		layer.msg("手机验证码发送成功，请注意查收");
		                var count = 60;
		                var interval = setInterval(function(){
		                	count -= 1;
		                	var detail = count + "s后可重发"
		                	$("input.forget_step_center_two_four").val(detail);
		                	$("input.forget_step_center_two_four").attr("disabled",true);
		                	if(count === 0){
		                		$("input.forget_step_center_two_four").val("发送验证码");
		                		$("input.forget_step_center_two_four").attr("disabled",false);
		                		clearInterval(interval);
		                	};  	
		                },1000);
		                
	            	}else{
	            		layer.msg("手机验证码验证失败，请重新输入");
	            	}
	            	
	            },
	            error:function(data){
	            	layer.msg("验证码发送失败，请检查手机号码是否有误");
	            },
	            complete:function (data) {
					
	            }
	        })
	});
}
//验证手机验证码
var CheckPhoneCode = function(){
		var phone = $("input.forget_step_center_one_two:eq(1)").val();
		var phonecode = $("input.forget_step_center_two_two:eq(1)").val();
		 $.ajax({
	            url:basePath+'/meb/checkphonecode',
	            data:{
	                phone:phone,
	                code:phonecode
	            },
	            type:'post',
	            dataType:'json',
	            success:function(data){
	            	if(data.code == "1"){
	            		
	            		$("div.forget_stepone").fadeOut();
	            		$("div.forget_steptwo").fadeOut();
	            		$("div.forget_stepthree").fadeIn();
	            	}else{
	            		layer.msg("验证手机验证码失败，请重新输入")
	            	}
	            },
	            error:function(data){
	            	layer.msg("验证手机验证码失败，请重新输入");
	            },
	            complete:function (data) {
					
	            }
	        });
}
//修改密码
var ChangePhonePW = function(){
	var phone = $("input.forget_step_center_one_two:eq(1)").val();
	var password = $("input.forget_step_center_one_two:eq(2)").val();
	console.log("phone="+phone);
	console.log("password="+password);
	$.ajax({
        url:basePath+'/meb/repassword',
        data:{
            phone:phone,
            password:password
        },
        type:'post',
        dataType:'json',
        success:function(data){
        	console.log(data);
        	var result = data.code;
        	if(result == "1"){
        		layer.alert("您已成功修改密码，请前往登录页登录",{icon: 1,title:"温馨提示"},function(index){         	
                	var url = basePath+"/loginstepone?loginstepflag=5";
            		url = encodeURI(url);
            		url = encodeURI(url);
            		window.location.href = (url);
                });
        	};
        	if(result == "2"){
        		layer.alert("修改密码失败，请重试",{icon: 1,title:"温馨提示"});
        	}
        	console.log("666");
        },
        error:function(data){
        	layer.msg("修改验证失败，请检查网络连接是否正常");
        },
        complete:function (data) {
			console.log(data);
        }
    })
}